import 'package:sqflite/sqflite.dart';

import 'storage_model.dart';
import 'table_action.dart';

class DbStorage {
  final Map<String, Database> dateBaseMap = {};

  static DbStorage? _instance;
  static DbStorage get instance => _getOrCreateInstance();
  static DbStorage _getOrCreateInstance() {
    if (_instance != null) {
      return _instance!;
    } else {
      _instance = DbStorage._();
      return _instance!;
    }
  }

  DbStorage._();
}

class JDStorageTool implements JDTableAction {
  ///数据库实例
  final Database database;
  JDStorageTool(this.database, String tableName, String sqlString) {
    tableAction = BaseTableAction(
        db: database, tableName: tableName, sqlString: sqlString);
  }
  BaseTableAction? tableAction;

  ///销毁数据库
  void destroy() {
    database.close();
  }

  @override
  Future<List<StorageModel>> getAllData() {
    return tableAction!.getAllData();
  }

  @override
  void saveData(StorageModel model) {
    tableAction?.saveData(model);
  }

  @override
  void deleteData(int id) {
    tableAction?.deleteData(id);
  }

  @override
  Future<int> getDataCount() {
    return tableAction!.getDataCount();
  }

  @override
  void update(StorageModel model) {
    tableAction?.update(model);
  }
}
